package cn.wk.libs4a.view.slidetab;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import cn.wk.libs4a.R;
import cn.wk.libs4a.WKBaseFragmentActivity;

/**
 * 滑动Tab栏基类
 */
public abstract class WKSlideTabAct extends WKBaseFragmentActivity {
	private WKPaperSlidingTabStrip tabs;
	private ViewPager pager;

	private WKFragmentPagerAdapter madapter;
	private ArrayList<String> titleList;

	/**
	 * 获取title列表
	 */
	public ArrayList<String> getTitleList() {
		return titleList;
	}

	/**
	 * 设置view销毁频率
	 */
	public void setOffscreenPageLimit(int i) {
		pager.setOffscreenPageLimit(i);
	}

	/**
	 * 获取 每页的View
	 */
	public abstract Fragment getItemView(int idx);

	/**
	 * 设置顶部tab栏目标题数组
	 */
	public abstract ArrayList<String> initTitleList();

	public void init() {
		titleList = new ArrayList<String>();
		tabs = (WKPaperSlidingTabStrip) findViewById(R.id.tabs);
		pager = (ViewPager) findViewById(R.id.pager);
		madapter = new WKFragmentPagerAdapter(getSupportFragmentManager());
		pager.setAdapter(madapter);
		final int pageMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
						.getDisplayMetrics());
		pager.setPageMargin(pageMargin);
		pager.setOffscreenPageLimit(1);

		tabs.setViewPager(pager);
		tabs.setTextColorResource(R.color.tab_text);
		tabs.setIndicatorColor(getResources().getColor(
				R.color.tab_btn_txt_selected));
		tabs.setTabPaddingLeftRight(4);
		tabs.setIndicatorHeight(5);
		tabs.setUnderlineHeight(2);
		tabs.setDividerPadding(20);// 设置中间竖线上下间距

		titleList.addAll(initTitleList());
		// madapter.getItem(0).onResume();
		madapter.notifyDataSetChanged();
		setOffscreenPageLimit(99);// 设置保留所有page数据
		// tabs.setTabClick(0);
		tabs.notifyDataSetChanged();
	}

	/**
	 * 更新标题
	 */
	public void updateTitle(ArrayList<String> list) {
		this.titleList = list;
		titleList.addAll(initTitleList());
		madapter.notifyDataSetChanged();
		tabs.notifyDataSetChanged();
	}

	/**
	 * Paper适配器
	 */
	class WKFragmentPagerAdapter extends FragmentPagerAdapter {

		public WKFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return getTitleList().get(position);
		}

		public String getPageId(int position) {
			return getTitleList().get(position);
		}

		@Override
		public int getCount() {
			return getTitleList().size();
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		@Override
		public Fragment getItem(int position) {
			return getItemView(position);
		}

	}

	@Override
	public void setData() {
		init();
	}

	/**
	 * 设置标题
	 */
	public void setTabTitleByIndex(int idx, String str) {
		if (idx >= titleList.size())
			return;
		titleList.set(idx, str);
		tabs.notifyDataSetChanged();
		madapter.notifyDataSetChanged();
	}

	/**
	 * 设置选中项字体颜色
	 * 
	 * @param color
	 */
	public void setTabTextColorSelected(int color) {
		tabs.setTabTextColorSelected(color);
	}

	/**
	 * 设置Tab选中字体大小
	 * 
	 * @param style
	 */
	public void setTabTextSizeSelected(int size) {
		tabs.setTabTextSizeSelected(size);
	}

	/**
	 * 设置Tab字体
	 * 
	 * @param style
	 */
	public void setTabTextStyle(int style) {
		tabs.setTabTextTypefaceStyle(style);
	}

	/**
	 * 设置Tab选中下划线高度
	 * 
	 * @param height
	 */
	public void setIndicatorHeight(int height) {
		tabs.setIndicatorHeight(height);
	}

	/**
	 * 设置Tab item中间竖线是否显示
	 * 
	 * @param height
	 */
	public void setLinesHave(boolean have) {
		tabs.tabLinesHave = have;
	}
	public WKFragmentPagerAdapter getAdapter(){
		return madapter;
	}
}
